package controllers;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

import models.SolutionAdoption;


public class AdoptionConnect {
	boolean hasEntries;
	private int iROWS;
	
	public AdoptionConnect(){
		hasEntries = false;
		iROWS = -1;
	}
	public boolean hasEntries(){
		return hasEntries;
	}
	
	public List<HashMap<String,Object>> getCompleteData(String... fieldNames) {
		List<HashMap<String, Object>> objList = new LinkedList<HashMap<String,Object>>();
		Objectify ofy = ObjectifyService.begin();
		iROWS = -1;
		
    	try {
    		Query<SolutionAdoption> q = ofy.query(SolutionAdoption.class);             
    		for ( SolutionAdoption f : q.fetch() ) {
    			HashMap<String,Object> itemMap = new HashMap<String,Object>();
    			for ( String fields : fieldNames) 
    			{
    				if ( fields.equalsIgnoreCase("adoptID") )   		{   itemMap.put("adoptID", f.getAdoptID()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("solID") )   			{   itemMap.put("solID", f.getSolID()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("solTitle") )   		{   itemMap.put("solTitle", f.getSolTitle()); hasEntries = true; }    				
    				if ( fields.equalsIgnoreCase("dtImplemented") ) 	{ 	itemMap.put("dtImplemented", f.getDtImplemented()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("adptTeamLead") ) 		{ 	itemMap.put("adptTeamLead", f.getAdptTeamLead()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("adptMembers") )  		{ 	itemMap.put("adptMembers", f.getAdptMembers());  hasEntries = true; }
    				if ( fields.equalsIgnoreCase("adptOthersMembers") )	{ 	itemMap.put("adptOthersMembers", f.getAdptOthersMembers());  hasEntries = true; }
    				if ( fields.equalsIgnoreCase("Customer") )  		{ 	itemMap.put("Customer", f.getCustomer()); hasEntries = true; }    				
    				if ( fields.equalsIgnoreCase("Location") ) 			{	itemMap.put("Location", f.getLocation()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("processArea") )		{	itemMap.put("processArea", f.getProcessArea()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("adptTeamPicKey") )	{	itemMap.put("adptTeamPicKey", f.getAdptTeamPicKey()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("easyRank") )			{	itemMap.put("easyRank", f.getEasyRank()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("achievedType") )		{	itemMap.put("achievedType", f.getAchievedType()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("achievedValue") )		{	itemMap.put("achievedValue", f.getAchievedValue()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("achievedUnits") )		{	itemMap.put("achievedUnits", f.getAchievedUnits()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("hardSavings") )		{	itemMap.put("hardSavings", f.getHardSavings()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("costAvoidance") )		{	itemMap.put("costAvoidance", f.getCostAvoidance()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("oppurtunityCost") )	{	itemMap.put("oppurtunityCost", f.getOppurtunityCost()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("impDays") )			{	itemMap.put("impDays", f.getImpDays()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("attachKey") )			{	itemMap.put("attachKey", f.getAttachKey()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("Comment") )			{	itemMap.put("Comment", f.getComment()); hasEntries = true; }    				
    				if ( fields.equalsIgnoreCase("submittedBY") )		{   itemMap.put("submittedBY", f.getSubmittedBY()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("submittedON") )		{	itemMap.put("submittedON", f.getSubmittedON()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("modifiedBY") )		{   itemMap.put("modifiedBY", f.getModifiedBY()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("modifiedON") )		{	itemMap.put("modifiedON", f.getModifiedON()); hasEntries = true; }
    				
    			}    			
				iROWS++;    			
    			objList.add(itemMap);
    		}
    	} catch ( Exception e ) { 
    		e.printStackTrace(); 
    	}
    	
    	return objList;	
	}
	
	public List<HashMap<String,Object>> getCompleteData() {
		List<HashMap<String, Object>> objList = new LinkedList<HashMap<String,Object>>();
		Objectify ofy = ObjectifyService.begin();
		iROWS = -1;
		
    	try {
    		Query<SolutionAdoption> q = ofy.query(SolutionAdoption.class);
 
    		for ( SolutionAdoption f : q.fetch() ) {
    			
        			HashMap<String,Object> itemMap = new HashMap<String,Object>();

    				itemMap.put("adoptID", f.getAdoptID());
    				itemMap.put("solID", f.getSolID()); 
    				itemMap.put("solTitle", f.getSolTitle());
    				itemMap.put("dtImplemented", f.getDtImplemented());
    				itemMap.put("adptTeamLead", f.getAdptTeamLead());
    				itemMap.put("adptMembers", f.getAdptMembers());
    				itemMap.put("adptOthersMembers", f.getAdptOthersMembers());
    				itemMap.put("Customer", f.getCustomer());    				
    				itemMap.put("Location", f.getLocation());
    				itemMap.put("processArea", f.getProcessArea());
    				itemMap.put("adptTeamPicKey", f.getAdptTeamPicKey());
    				itemMap.put("easyRank", f.getEasyRank());
    				itemMap.put("achievedType", f.getAchievedType());
    				itemMap.put("achievedValue", f.getAchievedValue());
    				itemMap.put("achievedUnits", f.getAchievedUnits());
    				itemMap.put("hardSavings", f.getHardSavings());
    				itemMap.put("costAvoidance", f.getCostAvoidance());
    				itemMap.put("oppurtunityCost", f.getOppurtunityCost());
    				itemMap.put("impDays", f.getImpDays());
    				itemMap.put("attachKey", f.getAttachKey());
    				itemMap.put("Comment", f.getComment());    				
    				itemMap.put("submittedBY", f.getSubmittedBY());
    				itemMap.put("submittedON", f.getSubmittedON());
    				itemMap.put("modifiedBY", f.getModifiedBY());
    				itemMap.put("modifiedON", f.getModifiedON());

    				
    				hasEntries = true;
    				iROWS++;
    				
    				objList.add(itemMap);
    		}
    	} catch ( Exception e ) { 
    		e.printStackTrace(); 
    	}
    	
    	return objList;	
	}

	public List<HashMap<String,Object>> getQuery(String fieldName, String Value) {
		List<HashMap<String, Object>> objList = new LinkedList<HashMap<String,Object>>();
		Objectify ofy = ObjectifyService.begin();
		iROWS = -1;
		
    	try {
    		Query<SolutionAdoption> q = ofy.query(SolutionAdoption.class).filter(fieldName, Value);
 
    		for ( SolutionAdoption f : q.fetch() ) {
    			
        			HashMap<String,Object> itemMap = new HashMap<String,Object>();

    				itemMap.put("adoptID", f.getAdoptID());
    				itemMap.put("solID", f.getSolID());
    				itemMap.put("solTitle", f.getSolTitle());
    				itemMap.put("dtImplemented", f.getDtImplemented());
    				itemMap.put("adptTeamLead", f.getAdptTeamLead());
    				itemMap.put("adptMembers", f.getAdptMembers());
    				itemMap.put("adptOthersMembers", f.getAdptOthersMembers());
    				itemMap.put("Customer", f.getCustomer());    				
    				itemMap.put("Location", f.getLocation());
    				itemMap.put("processArea", f.getProcessArea());
    				itemMap.put("adptTeamPicKey", f.getAdptTeamPicKey());
    				itemMap.put("easyRank", f.getEasyRank());
    				itemMap.put("achievedType", f.getAchievedType());
    				itemMap.put("achievedValue", f.getAchievedValue());
    				itemMap.put("achievedUnits", f.getAchievedUnits());
    				itemMap.put("hardSavings", f.getHardSavings());
    				itemMap.put("costAvoidance", f.getCostAvoidance());
    				itemMap.put("oppurtunityCost", f.getOppurtunityCost());
    				itemMap.put("impDays", f.getImpDays());
    				itemMap.put("attachKey", f.getAttachKey());
    				itemMap.put("Comment", f.getComment());    				
    				itemMap.put("submittedBY", f.getSubmittedBY());
    				itemMap.put("submittedON", f.getSubmittedON());
    				itemMap.put("modifiedBY", f.getModifiedBY());
    				itemMap.put("modifiedON", f.getModifiedON());
        			
    				
    				hasEntries = true;
    				iROWS++;
    				
    				objList.add(itemMap);
    		}
    	} catch ( Exception e ) { 
    		e.printStackTrace(); 
    	}
    	
    	return objList;	
	}
	
	
	public List<HashMap<String,Object>> getQuery(String fieldName, long Value) {
		List<HashMap<String, Object>> objList = new LinkedList<HashMap<String,Object>>();
		Objectify ofy = ObjectifyService.begin();
		iROWS = -1;
		
    	try {
    		
    		Query<SolutionAdoption> q = ofy.query(SolutionAdoption.class).filter(fieldName, Value);
 
    		for ( SolutionAdoption f : q.fetch() ) {
    			
        			HashMap<String,Object> itemMap = new HashMap<String,Object>();

    				itemMap.put("adoptID", f.getAdoptID());
    				itemMap.put("solID", f.getSolID());
    				itemMap.put("solTitle", f.getSolTitle());
    				itemMap.put("dtImplemented", f.getDtImplemented());
    				itemMap.put("adptTeamLead", f.getAdptTeamLead());
    				itemMap.put("adptMembers", f.getAdptMembers());
    				itemMap.put("adptOthersMembers", f.getAdptOthersMembers());
    				itemMap.put("Customer", f.getCustomer());    				
    				itemMap.put("Location", f.getLocation());
    				itemMap.put("processArea", f.getProcessArea());
    				itemMap.put("adptTeamPicKey", f.getAdptTeamPicKey());
    				itemMap.put("easyRank", f.getEasyRank());
    				itemMap.put("achievedType", f.getAchievedType());
    				itemMap.put("achievedValue", f.getAchievedValue());
    				itemMap.put("achievedUnits", f.getAchievedUnits());
    				itemMap.put("hardSavings", f.getHardSavings());
    				itemMap.put("costAvoidance", f.getCostAvoidance());
    				itemMap.put("oppurtunityCost", f.getOppurtunityCost());
    				itemMap.put("impDays", f.getImpDays());
    				itemMap.put("attachKey", f.getAttachKey());
    				itemMap.put("Comment", f.getComment());    				
    				itemMap.put("submittedBY", f.getSubmittedBY());
    				itemMap.put("submittedON", f.getSubmittedON());
    				itemMap.put("modifiedBY", f.getModifiedBY());
    				itemMap.put("modifiedON", f.getModifiedON());
    				
    				hasEntries = true;
    				iROWS++;
    				
    				objList.add(itemMap);
    		}
    	} catch ( Exception e ) { 
    		e.printStackTrace(); 
    	}
    	
    	return objList;	
	}
	
	public int getRows(){
		return iROWS;
	}
	
}